package com.jsrana.idea.plugin.ideaweather.widget;

import com.intellij.openapi.wm.CustomStatusBarWidget;
import com.intellij.openapi.wm.StatusBar;
import com.jsrana.idea.plugin.ideaweather.IdeaWeatherPlugin;
import com.jsrana.idea.plugin.ideaweather.ui.WeatherDialog;
import com.jsrana.idea.plugin.ideaweather.util.ImageUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * Created by jrana on 7/16/14.
 */
public class WeatherStatusBarWidget
        implements CustomStatusBarWidget {
    private JLabel label = new JLabel( "Loading Weather Data..." );
    WeatherDialog dialog = null;
    public static boolean showDialog = false;
    public static final String ID = "WeatherStatusBarWidget";

    public WeatherStatusBarWidget() {

    }

    @Override
    public JComponent getComponent() {
        BufferedImage image = null;
        try {
            image = ImageIO.read( getClass().getResource( "/resources/simple_weather_icon_01.png" ) );
            image = ImageUtil.resize( image, 20, 20 );
        }
        catch ( IOException e ) {
            e.printStackTrace();
        }

        if ( image != null ) {
            label.setIcon( new ImageIcon( image ) );
        }
        label.setText( "Loading Weather Data..." );
        label.addMouseListener( new MouseAdapter() {
            @Override public void mouseEntered( MouseEvent e ) {
                super.mouseEntered( e );
                IdeaWeatherPlugin.weatherWidgetMouseOverFlag = true;
            }

            @Override public void mouseExited( MouseEvent e ) {
                super.mouseExited( e );
                IdeaWeatherPlugin.weatherWidgetMouseOverFlag = false;
            }
        } );

        label.addMouseListener( new MouseAdapter() {
            @Override public void mouseReleased( MouseEvent e ) {
                super.mouseReleased( e );
                if ( !label.getText().equals( "Weather data not available" ) ) {
                    if ( dialog != null && dialog.isShowing() ) {
                        dialog.dispose();
                    }
                    else {
                        if ( dialog == null ) {
                            dialog = new WeatherDialog();
                            dialog.setSize( WeatherDialog.WIDTH, WeatherDialog.HEIGHT );
                            dialog.setUndecorated( true );
                            dialog.addFocusListener( new FocusAdapter() {
                                @Override public void focusLost( FocusEvent e ) {
                                    if ( !IdeaWeatherPlugin.weatherWidgetMouseOverFlag ) {
                                        super.focusLost( e );
                                        dialog.dispose();
                                    }
                                }
                            } );
                        }
                        else {
                            dialog.updateUI();
                        }

                        final JRootPane rootPane = SwingUtilities.getRootPane( label );
                        if ( rootPane != null && rootPane.isShowing() ) {
                            Point pos = label.getLocationOnScreen();
                            dialog.setLocation( pos.x - dialog.getWidth() + label.getWidth(), pos.y - dialog.getHeight() );
                        }
                        dialog.setFocusable( true );
                        dialog.setVisible( true );
                        dialog.requestFocus();
                    }
                }
            }
        } );

        return label;
    }

    @NotNull @Override public String ID() {
        return ID;
    }

    @Nullable @Override public WidgetPresentation getPresentation( @NotNull PlatformType platformType ) {
        return null;
    }

    @Override public void install( @NotNull StatusBar statusBar ) {
    }

    @Override public void dispose() {
    }

    public void setCondition( String condition,
                              String code ) {
        if ( condition == null || condition.length() == 0 ) {
            condition = "Loading Weather Data...";
        }
        label.setText( condition );
        if ( code == null || code.length() == 0 ) {
            showDialog = false;
            code = "30";
        }
        else {
            showDialog = true;
        }
        BufferedImage image = null;
        try {
            image = ImageIO.read( getClass().getResource( "/resources/plain/light/" + code + ".png" ) );
            image = ImageUtil.resize( image, 20, 20 );
        }
        catch ( IOException e ) {
            e.printStackTrace();
        }
        if ( image != null ) {
            label.setIcon( new ImageIcon( image ) );
        }
    }
}
